import { Geolocation } from '@ionic-native/geolocation';
import {EventHubService} from "../../services/event-hub.service";
import {IMapBusiness} from "./imap-business";
import {MapBusinessFactory} from "./map-business-factory";

export class AdpMapCommon{

    private geolocation: Geolocation;
    mapBusiness:IMapBusiness;

    constructor(){
        this.geolocation = new Geolocation();
        this.mapBusiness = MapBusinessFactory.getMapBusiness();
    }

    //定位用户当前位置
    getUserLatLng(option=null){
        let self = this;
        let zoom = 16;
        EventHubService.emit("map-geolocate-start",{
            mapApi:option.mapApi,
            geolocatedType:option.geolocatedType
        });
        // let geolocationCompeleted=(pos)=>{
        //
        //     if(!pos || !pos.coords){
        //         EventHubService.emit("map-geolocate-failured");
        //         return;
        //     }
        //     let lng = pos.coords.longitude;
        //     let lat = pos.coords.latitude;
        //     this.mapBusiness.convertLatLng(option.mapApi,lat,lng,data => {
        //         let center;
        //         if(data.status === 0) {
        //             center = data.points[0];
        //             self.mapBusiness.centerAndZoom(option.mapApi,center,zoom);
        //         }
        //         EventHubService.emit("map-geolocated",{
        //             mapApi:option.mapApi,
        //             center:center,
        //             zoom:zoom,
        //             geolocatedType:option.geolocatedType
        //         });
        //     });
        //
        // };
        //
        // this.geolocation.getCurrentPosition({enableHighAccuracy:true}).then(data => {
        //     geolocationCompeleted(data);
        // }).catch(error => {
        //     geolocationCompeleted(null);
        // });
        this.nativeGeolocate(option.mapApi,(res) => {
            if(res && res.lat && res.lng){
                self.mapBusiness.centerAndZoom(option.mapApi,res,zoom);
                EventHubService.emit("map-geolocated",{
                    mapApi:option.mapApi,
                    center:res,
                    zoom:zoom,
                    geolocatedType:option.geolocatedType
                });
            }else{

                //定位失败或者超时，模拟一个位置(上海)
                let center = new option.mapApi._mapApi.Point(121.475638,31.234576);
                self.mapBusiness.centerAndZoom(option.mapApi,center,zoom);
                EventHubService.emit("map-geolocated",{
                    mapApi:option.mapApi,
                    center:center,
                    zoom:zoom,
                    geolocatedType:option.geolocatedType
                });


                // EventHubService.emit("map-geolocate-failured");
            }
        })
    }

    //获取用户位置经纬度
    nativeGeolocate(mapApi,callback){

        let geolocationCompeleted=(pos)=>{
            if(!pos || !pos.coords){
                geolocationFailure("");
                return;
            }
            let lng = pos.coords.longitude;
            let lat = pos.coords.latitude;
            this.mapBusiness.convertLatLng(mapApi,lat,lng,data => {
                let center;
                if(data.status === 0) {
                    center = data.points[0];
                    callback(center);
                }else{
                    callback();
                }
            });

        };
        function geolocationFailure(error){
            // EventHubService.emit("map-geolocate-failured");
            callback(error);
        }

        this.geolocation.getCurrentPosition({enableHighAccuracy:true,timeout:20000}).then(data => {
            geolocationCompeleted(data);
        }).catch(error => {
            geolocationFailure(error);
        });
    }

}
